package com.xiaoyu.base.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import com.xiaoyu.base.entity.MessageEntity;

import java.util.List;

import static androidx.room.OnConflictStrategy.REPLACE;

@Dao
public interface MessageDao {

    @Insert(onConflict = REPLACE)
    Long upsert(MessageEntity message);

    @Insert(onConflict = REPLACE)
    List<Long> upsertAll(List<MessageEntity> messages);

    @Query("UPDATE message SET chatId = :chatId WHERE cid = :cid")
    void updateChatIdByLeanCid(String chatId, String cid);

    @Query("UPDATE message SET payloadVersion = :version, content = :content WHERE id = :id")
    void updatePayload(int version, String content, String id);

    @Query("UPDATE message SET invisible = 1 WHERE id = :id")
    void updateInvisible(String id);

    @Query("UPDATE message SET readReceipt = 2 WHERE chatId = :chatId and senderUid = :senderUid and readReceipt != 2 and time <= :maxTime")
    void updateReadReceipt(String chatId, String senderUid, long maxTime);

    @Query("UPDATE message SET text = :text WHERE id = :id")
    void updateText(String id, String text);

    @Query("SELECT * FROM message WHERE type = -1 AND invisible = 0 AND text LIKE '%' || :keyword || '%' LIMIT :limit")
    List<MessageEntity> searchTextMessage(String keyword, int limit);

    @Query("SELECT * FROM message WHERE chatId = :chatId AND type = -1 AND invisible = 0 AND text LIKE '%' || :keyword || '%' LIMIT :limit")
    List<MessageEntity> searchTextMessageByChatId(String chatId, String keyword, int limit);

    @Query("SELECT * FROM message WHERE id = :id LIMIT 1")
    MessageEntity getMessage(String id);

    @Query("SELECT * FROM message WHERE chatId = :chatId AND invisible = 0 AND (displayTime < :lastDisplayTime OR (displayTime = :lastDisplayTime AND id < :lastId)) ORDER BY displayTime DESC, id DESC limit :num")
    List<MessageEntity> getMessagesByChatId(String chatId, long lastDisplayTime, String lastId, int num);

    @Query("SELECT * FROM message WHERE chatId = :chatId AND invisible = 0 ORDER BY displayTime DESC, id DESC limit :num")
    List<MessageEntity> getMessagesByChatId(String chatId, int num);

    // for search
    @Query("SELECT * FROM message WHERE chatId = :chatId AND invisible = 0 AND (displayTime < :displayTime OR (displayTime = :displayTime AND id < :lastId)) ORDER BY displayTime DESC, id DESC LIMIT :num")
    List<MessageEntity> getMessagesByMessageIdAboveNum(String chatId, long displayTime, String lastId, int num);

    // for search
    @Query("SELECT * FROM message WHERE chatId = :chatId AND invisible = 0 AND (displayTime > :displayTime OR (displayTime = :displayTime AND id >= :lastId)) ORDER BY displayTime DESC, id DESC")
    List<MessageEntity> getMessagesByMessageId(String chatId, long displayTime, String lastId);

    @Delete
    void delete(MessageEntity... messages);

    @Query("DELETE FROM message WHERE chatId = :chatId")
    void deleteMessageByChatId(String chatId);

    @Query("DELETE FROM message")
    void deleteAllMessage();

    @Query("SELECT * FROM message WHERE chatId = :chatId and invisible = 0 ORDER BY displayTime DESC, id DESC LIMIT 1")
    MessageEntity getNewestMessage(String chatId);

    @Query("SELECT * FROM message WHERE chatId = :chatId and invisible = 0 AND invisibleLastContent = 0 ORDER BY displayTime DESC, id DESC LIMIT 1")
    MessageEntity getNewestLastContentMessage(String chatId);

    @Query("SELECT * FROM message WHERE chatId = :chatId and invisible = 0 and senderUid != :uid ORDER BY displayTime DESC, id DESC LIMIT 1")
    MessageEntity getNewestReceiveMessage(String chatId, String uid);

    @Query("SELECT count(1) FROM message WHERE chatId = :chatId and senderUid = :senderUid and idType != 1")
    int getSendSuccessMessageCount(String chatId, String senderUid);
}
